<!--{template lyear:header_simple_start}-->
<link rel="stylesheet" href="static/lyear/js/layui/css/layui.css">
<style>
	.icon {
		width: 30px;
		height: 30px;
	}
	.iconFirstWord {
		width: 18px;
		height: 18px;
		line-height: 16px;
		font-size: 12px;
	}
</style>
<!--{template lyear:header_simple_end}-->
<main class="bs-main-container">
	<div class="container-fluid">
		<div class="card">
			<div class="card-body">
				<div class="card-search mb-2-5">
					  <div class="row">
						<div class="col-md-8">
							<div class="input-group mb-3">
								<span class="input-group-text">{lang share_title}</span>
								<input type="text" class="form-control" name="keyword" id="keyword" placeholder="{lang share_title}" aria-label="{lang share_title}">
								<!--{if $_G['adminid']}--><span class="input-group-text">{lang username}</span>
								<input type="text" class="form-control" name="username" id="username" placeholder="{lang username}" placeholder="{lang username}" aria-label="{lang username}"><!--{/if}-->
							  </div>
						</div>
						<div class="col-md-4">
						  <button type="submit" class="btn btn-primary me-1" id="search-btn">搜索</button>
						  <button type="reset" class="btn btn-default" id="refreshsearch">重置</button>
						</div>
					  </div>
				  </div>
                <table class="layui-hide" id="table" lay-filter="table"></table>
				<script type="text/html" id="toolbar">
					<div class="layui-btn-container">
						<button type="button" class="layui-btn layui-btn-normal" lay-event="getCheckData">获取选中行数据</button>
						<button type="button" class="layui-btn layui-btn-danger" lay-event="delete">{lang del_share}</button>
						<!--{if $_G['adminid']}--><button type="button" class="layui-btn layui-bg-orange" lay-event="forbidden">{lang screen_share}</button>
						<button type="button" class="layui-btn" lay-event="allow">{lang cancel_shielding}</button><!--{/if}-->
						<button type="button" class="layui-btn layui-btn-normal" lay-event="get">刷新</button>
					</div>
				</script>
				<script type="text/html" id="barDemo">
					<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">更多</a>
					<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="copy">复制信息</a>
				</script>
			</div>
		</div>
	</div>
</main>
<script src="static/lyear/js/layui/layui.js"></script>
<script>
	var type = '';
	layui.use('table', function(){
	  var table = layui.table;
	table.render({
	elem: '#table',
	url: '{MOD_URL}&do=getinfo',
	title: '分享管理数据表格',
	toolbar: '#toolbar',
	autoSort: false,
	text: {
		none: '暂无相关数据'
	},
	page: {
		limit: 20, // 默认显示20行
		limits: [10, 20, 50, 100, 200, 500, 900] // 可选的行数
	},
	cellMinWidth: 80 //全局定义常规单元格的最小宽度
	,cols: [[
	{type:'checkbox', fixed: 'left'}
		,{field:'id', title: 'ID',width:80, fixed: 'left'}
		,{field:'title', title: '{lang sharing_file}', align: 'center', sort: true}
		,{field:'dateline', title: '{lang share_time}', align: 'center', sort: true}
		,{field:'count', title: '{lang view_count}', align: 'center', sort: true}
		,{field:'downs', title: '下载次数', align: 'center', sort: true}
		,{field:'password', title: '{lang extract_password}', align: 'center', sort: true}
		,{field:'type', title: '{lang file_type}', align: 'center', sort: true}
		<!--{if $_G['adminid']}-->,{field:'username', title: '{lang share_user}', align: 'center', sort: true,templet: function(d) {
		return d.username;
	}}<!--{/if}-->
	,{field:'status', title: '状态', align: 'center', sort: true,templet: function(d) {
		return d.status;
	}}
		,{title: '{lang operation}',width:140, align:'center', toolbar: '#barDemo', fixed: 'right'}
	]],
	initSort: {
		field: 'dateline', // 默认排序字段
		type: 'desc' // 默认排序方式
	}
	});
	//头工具栏事件
	table.on('toolbar(table)', function(obj){
		var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
		var data = checkStatus.data;
		switch(obj.event){
			case 'getCheckData':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				// 创建模板字符串
				var template = '<div class="accordion">';
				data.forEach(function(item) {
					template += `<div class="accordion-item">`;
					template += `<h2 class="accordion-header" id="Open-heading${item.id}">`;
					template += `<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#Open-collapse${item.id}" aria-expanded="true" aria-controls="Open-collapse${item.id}">`;
					template += `{lang sharing_file}：${item.title}`;
					template += `</button>`;
					template += `</h2>`;
					template += `<div id="Open-collapse${item.id}" class="accordion-collapse collapse show" aria-labelledby="Open-heading${item.id}">`;
					template += `<div class="accordion-body">`;
					template += `<p><strong>ID:</strong> ${item.id}</p>`;
					template += `<p><strong>{lang sharing_file}:</strong> ${item.title}</p>`;
					template += `<p><strong>{lang share_time}:</strong> ${item.dateline}</p>`;
					<!--{if $_G['adminid']}-->template += `<p><strong>{lang share_user}:</strong> ${item.username}</p>`;<!--{/if}-->
					if (item.sharelink) {
						template += `<p><strong>{lang link}:</strong> <a href="${item.sharelink}" target="_blank">${item.sharelink}</a></p>`;
					}
					if (item.password) {
						template += `<p><strong>{lang extract_password}:</strong> ${item.password}</p>`;
					}
					if (item.endtime) {
						template += `<p><strong>{lang expiration_time}:</strong> ${item.endtime}</p>`;
					}
					if (item.count) {
						template += `<p><strong>{lang view_count}:</strong> ${item.count}{lang degree}</p>`;
					}
					if (item.downs) {
						template += `<p><strong>下载次数:</strong> ${item.downs}{lang degree}</p>`;
					}
					if (item.status) {
						template += `<p><strong>状态:</strong> ${item.status}</p>`;
					}
					if (item.number) {
						template += `<p><strong>{lang limited_number}:</strong> ${item.number}</p>`;
					}
					template += `</div>`;
					template += `</div>`;
					template += `</div>`;
				});
				template += '</div>';
				layer.alert(template, {
					title: '选中的文件数据',
					btn: ['确定']
				});
				break;
				case 'delete':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var sids = new Array();
				$.each(data,function(i) {
					sids.push(this.sid);
				});
				layer.confirm('{lang share_stop_sharing1}', {title: '{lang affirm_stop_sharing}',skin:'lyear-skin-danger'}, function(index){
					var loading = layer.load(0, { shade:  [0.1, '#000'] });
					jQuery.post('{MOD_URL}&op=ajax&do=delete', {'sids': sids},function(json){
						layer.close(loading);
						layer.close(index);
						for(var o in json.msg){
							if(json.msg[o]['success']){
								showmessage('删除成功', 'success', '3000', 1);
							} else if(json.msg[o]['error']){
								showmessage(json.msg[o]['error'], 'danger', '3000', 1);
							} else {
								showmessage('{lang error}', 'danger', '3000', 1);
							}
						}
						table.reload('table');
					},'json').fail(function() {
							layer.close(loading);
							layer.close(index);
							showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
						});
				});
				break;
				case 'forbidden':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var sids = new Array();
				$.each(data,function(i) {
					sids.push(this.sid);
				});
				layer.confirm('{lang share_stop_sharing2}', {title: '{lang affirm_screen_share}',skin:'lyear-skin-warning'}, function(index){
					var loading = layer.load(0, { shade:  [0.1, '#000'] });
					jQuery.post('{MOD_URL}&op=ajax&do=forbidden', {'sids': sids, "flag": 'forbidden'},function(json){
						layer.close(loading);
						layer.close(index);
						if(json.msg == 'success'){
							showmessage('删除成功', 'success', '3000', 1);
						} else if(json.error){
								showmessage(json.error, 'danger', '3000', 1);
						} else {
							showmessage('{lang shielding_failure}', 'danger', '3000', 1);
						}
						table.reload('table');
					},'json').fail(function() {
							layer.close(loading);
							layer.close(index);
							showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
						});
				});
				break;
				case 'allow':
				if (data.length === 0) {
					showmessage("请至少选择一行", 'danger', 3000, 1);
					return;
				}
				var sids = new Array();
				$.each(data,function(i) {
					sids.push(this.sid);
				});
				layer.confirm('{lang share_stop_sharing3}', {title: '{lang affirm_cancel_shielding}',skin:'lyear-skin-info'}, function(index){
					var loading = layer.load(0, { shade:  [0.1, '#000'] });
					jQuery.post('{MOD_URL}&op=ajax&do=forbidden', {'sids': sids, "flag": 'allow'},function(json){
						layer.close(loading);
						layer.close(index);
						if(json.msg == 'success'){
							showmessage('删除成功', 'success', '3000', 1);
						} else if(json.error){
								showmessage(json.error, 'danger', '3000', 1);
						} else {
							showmessage('{lang cancel_shielding_failure}', 'danger', '3000', 1);
						}
						table.reload('table');
					},'json').fail(function() {
							layer.close(loading);
							layer.close(index);
							showmessage('服务器发生错误，请稍后再试', 'danger',3000,1);
						});
				});
				break;
				case 'get':
				layui.table.reload('table', {
					where: {type: type}, // 清空搜索参数
					page: {
						curr: 1 //重新从第 1 页开始
					}
				});
				break; 
			};
		});
	  //监听工具条
	  table.on('tool(table)', function(obj){
		var data = obj.data;
		if(obj.event === 'detail'){
			showDataTemplate(obj);
		} else if(obj.event === 'copy'){
			copyShareInfo(data);
		}
	  });
	  //触发排序事件 
	table.on('sort(table)', function(obj){
		table.reload('table', {
			initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
			,where: {
				field: obj.field //排序字段
				,order: obj.type //排序方式
			}
		});
	});
	  table.on('rowDouble(table)', function(obj){
		showDataTemplate(obj);
	  });
		function showDataTemplate(obj) {
			var data = obj.data;
			var template = `
				<div>
					<p><strong>ID:</strong> ${data.id}</p>
					<p><strong>{lang sharing_file}:</strong> ${data.title}</p>
					<p><strong>{lang share_time}:</strong> ${data.dateline}</p>
				<!--{if $_G['adminid']}-->${data.username ? `<p><strong>{lang share_user}:</strong> ${data.username}</p>` : ''}<!--{/if}-->
				${data.sharelink ? `<p><strong>{lang link}:</strong> <a href="${data.sharelink}">${data.sharelink}</a></p>` : ''}
				${data.password ? `<p><strong>{lang extract_password}:</strong> ${data.password}</p>` : ''}
				${data.endtime ? `<p><strong>{lang expiration_time}:</strong> ${data.endtime}</p>` : ''}
				${data.count ? `<p><strong>{lang view_count}:</strong> ${data.count}{lang degree}</p>` : ''}
				${data.downs ? `<p><strong>下载次数:</strong> ${data.downs}{lang degree}</p>` : ''}
				${data.status ? `<p><strong>状态:</strong> ${data.status}</p>` : ''}
				${data.number ? `<p><strong>{lang limited_number}:</strong> ${data.number}</p>` : ''}
				</div>
			`;
	
			layer.alert(JSON.stringify(data), {
				title: `${data.title} 详情：`,
				content: template,
			});
	
		// 标注选中样式
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
		}
		function copyShareInfo(data) {
			// 创建临时的 input 元素用于复制链接
			var tempInput = document.createElement("input");
			document.body.appendChild(tempInput);
			var copyText = data.title + "\n " + data.sharelink;
			if (data.password) {
				copyText += " \n密码: " + data.password;
			}
			tempInput.value = copyText;
			tempInput.select();
			document.execCommand("copy");
			document.body.removeChild(tempInput);
			layer.msg('信息已复制到剪贴板');
		}
		// 搜索按钮功能
		$(document).on('click', '#search-btn', function() {
		// 获取搜索关键词
		var keyword = $('#keyword').val();
		var username = $('#username').val();
		// 刷新表格并传递搜索参数
		table.reload('table', {
		  where: { keyword: keyword,username:username},
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	   // 重置按钮功能
	   $('#refreshsearch').click(function() {
		// 清空表单内容
        $('#username').val('');
        $('#keyword').val('');
		// 刷新表格
		table.reload('table', {
		  where: {type: type}, // 清空搜索参数
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	  // 类型按钮功能
	  $(document).on('click', '.type-btn', function() {
		// 移除所有 .type-btn 的 active 类
		$('.type-btn').removeClass('active');
		// 为当前点击的按钮添加 active 类
		$(this).addClass('active');
		$('#keyword').val('');
		$('#username').val('');
		// 获取当前的 type 值
		operation = $(this).data('operation');
		type = $(this).data('type');
		// 刷新表格并传递 operation 参数
		table.reload('table', {
		  where: {type: type},
		  page: {
			curr: 1 //重新从第 1 页开始
		  }
		});
	  });
	});
</script>
<!--{template lyear:footer_simple}-->